//package com.qiyang.login.config;
//
//import com.alibaba.fastjson.JSON;
//import com.google.common.base.Optional;
//import com.netflix.loadbalancer.*;
//import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
//import com.qiyang.login.filter.SpcZonePreferenceServerListFilter;
//import org.apache.commons.lang.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpSession;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * Created on 2018/5/14.
// *
// * @author sehuaqi.
// * 描述：
// */
//public class SpcRandomRule extends ClientConfigEnabledRoundRobinRule {
//
//    Logger logger = LoggerFactory.getLogger(SpcRandomRule.class);
//
////    @Override
////    public Server choose(Object key) {
////
////        logger.info(">>>>>进入重写路由规则机制");
////
////        HttpServletRequest request = getRequest();
////
////        //logger.info("request = " + JSON.toJSONString(request));
////
////        // user1--->lable
////
////        List<Server> newServers = new ArrayList<>();
////
////        ILoadBalancer lb = getLoadBalancer();
////
////        List<Server> allServers = lb.getAllServers();
////
////        String userTag = request.getHeader("user_tag");
////        HttpSession session = request.getSession();
////
////        Object userTagObj = session.getAttribute("user_tag");
////
////
////        logger.info("sessionId={}", session.getId());
////        if (null != userTagObj) {
////            userTag = userTagObj.toString();
////        } else {
////            session.setAttribute("user_tag", userTag);
////        }
////
////
////        if (StringUtils.isNotBlank(userTag)) {
////            for (Server server : allServers) {
////                logger.info("host:port{}", server.getHostPort());
////                if (server instanceof DiscoveryEnabledServer) {
////                    DiscoveryEnabledServer discoveryEnabledServer = (DiscoveryEnabledServer) server;
////                    String customParam = discoveryEnabledServer.getInstanceInfo().getMetadata().get("custom-param");
////                    String serverTag = discoveryEnabledServer.getInstanceInfo().getMetadata().get("server-tag");
////                    logger.info("customParam={},version={}", customParam, serverTag);
////
////                    if (userTag.equals(serverTag)) {
////                        newServers.add(server);
////                        logger.info(">>>>>服务调用实例筛选，{} 符合条件", server.getHostPort());
////                    } else {
////                        logger.info(">>>>>服务调用实例筛选，{} 不符合条件", server.getHostPort());
////                    }
////                }
////            }
////        } else {
////            newServers = allServers;
////        }
////
////
////        Optional<Server> server = getPredicate().chooseRoundRobinAfterFiltering(newServers, key);
////        if (server.isPresent()) {
////            return server.get();
////        } else {
////            return null;
////        }
////    }
//
//    public static ServletRequestAttributes getRequestAttributes() {
//
//        try {
//            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//            ServletRequestAttributes sra = null;
//
//            if (null != requestAttributes) {
//                sra = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
//            }
//
//            return sra;
//        } catch (Exception e) {
//            return null;
//        }
//    }
//
//    public static HttpServletRequest getRequest() {
//
//        try {
//            ServletRequestAttributes requestAttributes = getRequestAttributes();
//            HttpServletRequest request = null;
//
//            if (null != requestAttributes) {
//                request = requestAttributes.getRequest();
//            }
//
//            return request;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
//}
